<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-s-tools"></i>基础数据管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-plus" @click="addVisible=true">添加</el-button>
            </div>
            <el-table
                v-loading="loading"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column label="序号" width="55" align="center">
                    <template slot-scope="scope">{{scope.$index+1}}</template>
                </el-table-column>
                <el-table-column prop="name" label="名称" align="center"></el-table-column>
                <el-table-column prop="value" label="值" align="center"></el-table-column>
                <el-table-column prop="type" label="类型" align="center"></el-table-column>
                <el-table-column prop="description" label="描述" align="center"></el-table-column>
                <el-table-column prop="remarks" label="备注" align="center"></el-table-column>
                <el-table-column prop="sort" label="排序" align="center"></el-table-column>
                <el-table-column prop="updatedBy" label="维护人" align="center"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 添加弹出框 -->
        <el-dialog title="添加" :visible.sync="addVisible" width="700px">
            <AddDialog @addBase="addBase" :addVisible="addVisible" />
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="700px">
            <EditDialog @editBase="editBase" :editOneData="editOneData"/>
        </el-dialog>
    </div>
</template>

<script>
import AddDialog from '@/components/BaseManage/addDialog.vue';
import EditDialog from '@/components/BaseManage/editDialog.vue';
import { baseFindList, baseAdd, baseUpdate, baseDelete } from 'services/BaseManage';
export default {
    name: 'basetable',
    components: { AddDialog, EditDialog },
    data() {
        return {
            loading: true,
            query: {
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [
                {
                    name: '1',
                    value: '狂的历史',
                    type: 'http://img1.imgtn.bdimg',
                    description: '213213',
                    remarks: '狂的历史',
                    sort: 'http://img1.imgtn.bdimg',
                    updatedBy: '213213'
                }
            ],
            addVisible: false,
            editVisible: false,
            editOneData: {},
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        async editBase(params) {
            const res = await baseUpdate(params);
            if (res.code === '0') {
                this.$message({
                    message: '提交成功',
                    type: 'success'
                });
                this.editVisible = false;
                this.getData();
            } else {
                this.$message.error('操作错误');
            }
        },
        async addBase(params) {
            const res = await baseAdd(params);
            if (res.code === '0') {
                this.$message({
                    message: '提交成功',
                    type: 'success'
                });
                this.addVisible = false;
                this.getData();
            } else {
                this.$message.error('操作错误');
            }
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            this.loading = true;
            baseFindList(this.query).then(res => {
                this.loading = false;
                this.tableData = res.data;
                this.pageTotal = res.pageTotal;
            });
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.baseDelete(row);
                })
                .catch(() => {});
        },
        async baseDelete(row) {
            const res = await baseDelete({ id: row.id });
            if (res.code === '0') {
                this.getData();
            }
        },
        // 编辑操作
        handleEdit(index, row) {
            this.editOneData = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.mr10 {
    margin-right: 10px;
    margin-left: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>